const commentService = require('../service/comment.service');

class CommentController {
  async getList(ctx) {
    const { momentId, pageSize = 10, pageNumber = 1 } = ctx.query;
    const result = await commentService.getCommentByMomentId({
      momentId,
      pageSize,
      pageNumber,
    });
    ctx.body = { errorCode: 0, data: result };
  }
  async create(ctx) {
    const { id: userId } = ctx.user;
    const { content, momentId } = ctx.request.body;
    const result = await commentService.create({ content, userId, momentId });
    ctx.body = { errCode: 0, data: result };
  }
  async reply(ctx) {
    try {
      const { id: userId } = ctx.user;
      const { content, momentId, commentId } = ctx.request.body;
      const result = await commentService.reply({
        content,
        userId,
        momentId,
        commentId,
      });
      ctx.body = { errCode: 0, data: result };
    } catch (error) {
      ctx.body = { errCode: -2, data: 'sql 执行异常', error };
    }
  }
  async update(ctx) {
    const { id } = ctx.params;
    const { content } = ctx.request.body;
    const result = await commentService.update(id, content);
    if (result.affectedRows) {
      ctx.body = { errCode: 0, data: result };
    } else {
      ctx.body = { errCode: -1, data: 'fail' };
    }
  }
  async remove(ctx) {
    const { id } = ctx.params;
    const result = await commentService.remove(id);
    if (result.affectedRows) {
      ctx.body = { errCode: 0, data: result };
    } else {
      ctx.body = { errCode: -1, data: 'fail' };
    }
  }
}

module.exports = new CommentController();
